/**
 * 
 */
package ru.opedge.notifical;

import java.io.FileInputStream;
import java.util.Properties;
import java.util.logging.FileHandler;
import java.util.logging.Logger;

import ru.opedge.notifical.bot.BotException;
import ru.opedge.notifical.bot.INotificationBot;
import ru.opedge.notifical.bot.xmpp.XMPPNotificationBot;
import ru.opedge.notifical.notification.service.AuthException;
import ru.opedge.notifical.notification.service.INotificationService;
import ru.opedge.notifical.notification.service.google.GCalService;
import ru.opedge.notifical.notification.service.google.GCalSmsService;

/**
 * @author opedge
 * 
 */
public class App
{
	public static final String APP_NAME = "notifical-1.0";

	public static final String DEFAULT_CONFIG_FILENAME = "config.properties";

	public static final String HELP_ARG = "-h";

	private static Logger logger = Logger.getLogger("ru.opedge.notifical");

	public static INotificationService createNotificationService(
			final String username, final String password) throws AuthException
	{
		final GCalService service = new GCalSmsService(APP_NAME);

		logger.info("Trying to connect to google calendar...");
		try
		{
			service.setUserCredentials(username, password);
		}
		catch (final AuthException e)
		{
			logger.warning("Connection fail...");
			throw e;
		}
		logger.info("Successful connection.");
		return service;
	}

	public static INotificationBot createNotificationBot(final String jid,
			final String password, final String host, final int port,
			final String server, final String resource, final int delay,
			final INotificationService service) throws BotException
	{
		final INotificationBot bot = new XMPPNotificationBot(host, port,
				server, resource, service, delay);

		logger.info("Trying to connect to jabber server...");
		try
		{
			bot.connect(jid, password);
		}
		catch (final BotException e)
		{
			logger.warning("Connection fail...");
			throw e;
		}
		logger.info("Successful connection.");
		return bot;
	}

	public static String getHelp()
	{
		return "Jabber notification bot. Use google calendar to send sms notifications.\n"
				+ "Usage: java -jar notifical.jar [argument]\n"
				+ "Argument:\n"
				+ "\t-h display this message\n"
				+ "\t<PATH> path in filesystem to config file "
				+ "(config.properties by default)";
	}

	/**
	 * @param args
	 */
	public static void main(final String[] args)
	{
		final Properties config = new Properties();
		INotificationBot bot = null;
		String configFilename = null;
		try
		{
			try
			{
				if (args[0].equals(HELP_ARG))
				{
					System.out.println(getHelp());
					System.exit(0);
				}
				else
					configFilename = args[0];
			}
			catch (final ArrayIndexOutOfBoundsException e)
			{
				configFilename = DEFAULT_CONFIG_FILENAME;
			}

			config.load(new FileInputStream(configFilename));

			final String logFilename = config.getProperty("log.filename");
			if (!logFilename.equals(""))
				logger.addHandler(new FileHandler(logFilename));

			final String googleUsername = config.getProperty("google.username");
			final String googlePassword = config.getProperty("google.password");

			final INotificationService service = createNotificationService(
					googleUsername, googlePassword);

			final String host = config.getProperty("jabber.host");
			final int port = Integer
					.parseInt(config.getProperty("jabber.port"));
			final String resource = config.getProperty("jabber.resource");
			final String jid = config.getProperty("jabber.id");
			final String jabberPassword = config.getProperty("jabber.password");
			final String serverName = config.getProperty("jabber.server");
			final int delay = Integer.parseInt(config
					.getProperty("notification.delay"));

			bot = createNotificationBot(jid, jabberPassword, host, port,
					serverName, resource, delay, service);
		}
		catch (final Exception e)
		{
			logger.warning((e.getMessage() != null ? e.getMessage() + " " : "")
					+ "Exiting application. Bye.");
			bot.disconnect();
			System.exit(0);
		}

		while (bot.isConnected())
			try
			{
				Thread.sleep(1000);
			}
			catch (final InterruptedException e)
			{
				logger.info("Exiting application. Bye.");
				bot.disconnect();
			}
	}
}
